#!groovy

@Library('SovrinHelpers@v2.2.1') _

String name = 'indy-node'
String pkgName = name
String mainModuleName = 'indy_node'
Boolean gatherLogs = (params.GATHER_LOGS ?: env.GATHER_LOGS) != 'false'

def nodeTestUbuntu = {
    try {
        echo 'Ubuntu Test: Checkout csm'
        checkout scm

        echo 'Ubuntu Test: Build docker image'
        def testEnv = dockerHelpers.build(name)

        testEnv.inside('--network host') {
            echo 'Ubuntu Test: Install dependencies'
            testHelpers.install()

            echo 'Ubuntu Test: Test'
            testHelpers.testRunner([resFile: "test-result-node.${NODE_NAME}.txt", testDir: 'indy_node'])
            //testHelpers.testJUnit(resFile: "test-result-node.${NODE_NAME}.xml")
        }
    }
    finally {
        echo 'Ubuntu Test: Cleanup'
        step([$class: 'WsCleanup'])
    }
}

def commonTestUbuntu = {
    try {
        echo 'Ubuntu Test: Checkout csm'
        checkout scm

        echo 'Ubuntu Test: Build docker image'
        def testEnv = dockerHelpers.build(name)

        testEnv.inside {
            echo 'Ubuntu Test: Install dependencies'
            testHelpers.install()

            echo 'Ubuntu Test: Test'
            testHelpers.testJUnit([resFile: "test-result-common.${NODE_NAME}.xml", testDir: 'indy_common'])
        }
    }
    finally {
        echo 'Ubuntu Test: Cleanup'
        step([$class: 'WsCleanup'])
    }
}

def buildDebUbuntu = { releaseVersion, sourcePath, packageVersion=null, missedPkgs=false ->
    def volumeName = "$name-deb-u1604"
    packageVersion = packageVersion ?: releaseVersion

    if (env.BRANCH_NAME != '' && env.BRANCH_NAME != 'master') {
        volumeName = "${volumeName}.${BRANCH_NAME}"
    }
    if (sh(script: "docker volume ls -q | grep -q '^$volumeName\$'", returnStatus: true) == 0) {
        sh "docker volume rm $volumeName"
    }

    // TODO build only missed ones
    dir('build-scripts/ubuntu-1604') {
        sh "./build-$name-docker.sh \"$sourcePath\" $releaseVersion $volumeName $packageVersion"
        if (missedPkgs == [pkgName]) {
            echo "Skip 3rd parties building"
        } else {
            sh "./build-3rd-parties-docker.sh $volumeName"
        }
    }
    return "$volumeName"
}

def systemTests = { component, releaseVersion ->
    def localLib
    nodeWrapper('ubuntu') {
        stage('Load local shared library') {
            checkout scm
            localLib = load 'ci/pipeline.groovy'
        }
    }

    localLib.systemTests {
        repoChannel = component
        pkgVersion = releaseVersion
        testSchema = [
            ['test_ledger.py'],
            ['TestViewChangeSuite.py'],
            ['TestConsensusSuite.py'],
            ['test_state_proof.py']
        ]
        testVersion = 'v0.8.43'
        testVersionByTag = true
        delegate.gatherLogs = gatherLogs
    }
}

def options = new TestAndPublishOptions()
options.setPkgName(pkgName)
options.setApprovers(['QA'])
options.setNotifEmails([
    QA: [
        to: "${env.INDY_NODE_QA_RECIPIENTS ?: ''}",
        cc: "${env.INDY_NODE_RECIPIENTS ?: ''}"
    ],
    success: [
        to: "${env.INDY_NODE_RECIPIENTS ?: ''}"
    ],
    fail: [
        to: "${env.INDY_NODE_RECIPIENTS ?: ''}"
    ]
])

// TODO duplicates list from build scripts
options.setBuiltPkgs([
    'python3-timeout-decorator': '0.4.0',
    'python3-distro': '1.3.0',
])


options.enable([StagesEnum.PACK_RELEASE_COPY, StagesEnum.PACK_RELEASE_COPY_ST])
options.setCopyWithDeps(true)
options.setSystemTestsCb(systemTests)
options.setPrContexts([env.INDY_GITHUB_PR_REQUIRED_CONTEXT ?: "ci/hyperledger-jenkins/pr-merge"])

testAndPublish(
    name,
    [
        ubuntu: [node: nodeTestUbuntu, common: commonTestUbuntu]
    ],
    true, options, [ubuntu: buildDebUbuntu], mainModuleName
)
